/**
 * Project Name:fastPlat
 * File Name:SiteMsgController.java
 * Package Name:com.fast.cms.web.controller.article
 * Date:2017年05月27日 13时58分27秒
 * Copyright (c) 2017, lsflu@126.com All Rights Reserved.
*/

package com.fast.cms.web.controller.article;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fast.base.db.page.PageView;
import com.fast.base.db.sql.AbstractSqlGenerator;
import com.fast.base.model.AnoSysLog;
import com.fast.base.util.LogUtils;
import com.fast.base.util.StringUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.ResponseEnum;
import com.fast.base.web.BaseController;
import com.fast.cms.model.article.SiteMsg;
import com.fast.cms.service.article.SiteMsgService;
import com.fast.cms.web.form.SiteMsgForm;
import com.fast.common.userCache.AdminCacheUtils;
import com.fast.common.userCache.UserRequestUtils;

/**
 * ClassName:SiteMsgController <br/>
 * 用途: 站内信 web控制类 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @version  1.0
 * @since    JDK 1.6
 * @see
 * Date:     2017年05月27日 13时58分27秒 <br/> 	 
 */
@Controller
@RequestMapping("/siteMsg")
public class SiteMsgController extends BaseController{
	
	@Resource(name="siteMsgService")
	private SiteMsgService siteMsgService;

	/**
	 * 编辑站内信信息
	 * @param imageUrl
	 * @return
	 */
	@RequestMapping("/editSiteMsg.do")
	@ResponseBody
	@AnoSysLog(actionName="编辑站内信信息")
	public ResponseData editSiteMsg(){
		ResponseData responseData = new ResponseData();
		SiteMsg siteMsg = null;
		try {
			int id = webUtil.getInt("id");
			if(id>0){
				siteMsg = this.siteMsgService.getById(SiteMsg.class,id);
				siteMsg = (SiteMsg) webUtil.bindDataToBean(siteMsg);
			}else{
				siteMsg = new SiteMsg();
				siteMsg = (SiteMsg) webUtil.bindDataToBean(siteMsg);
				UserRequestUtils.bindCreaterInfo(siteMsg);
			}
			UserRequestUtils.bindEditorInfo(siteMsg);
			if(this.siteMsgService.merge(siteMsg)!=null){
				responseData.buildResult(ResponseEnum.EDIT_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.EDIT_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 删除站内信信息到回收站
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delSiteMsgByIdForHS.do")
	@AnoSysLog(actionName="删除站内信到回收站")
	public ResponseData delSiteMsgByIdForHS(int id) {
		LogUtils.info("delSiteMsgByIdForHS id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			String sql = "update "+AbstractSqlGenerator.getTableName(SiteMsg.class)+" set delete_Flag=1 where id="+id;
			boolean actionFlag = siteMsgService.modifyRow(sql)>0;
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delSiteMsgByIdForHS responseData=:"+responseData.toString());
		return responseData;
	}

	/**
	 * 删除站内信信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delSiteMsgById.do")
	@AnoSysLog(actionName="彻底删除站内信")
	public ResponseData delSiteMsgById(int id) {
		LogUtils.info("delSiteMsgById id=:"+id);
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = siteMsgService.deleteById(SiteMsg.class, id);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		LogUtils.info("delSiteMsgById responseData=:"+responseData.toString());
		return responseData;
	}
	
	/**
	 * 删除多个站内信信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/batchDelSiteMsg.do")
	@AnoSysLog(actionName="批量删除站内信评论")
	public ResponseData batchDelSiteMsg(String ids){
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = siteMsgService.deleteByIds(SiteMsg.class, ids);
			if(actionCount>0){
				responseData.buildResult(ResponseEnum.DELETE_SUCCESS);
			}else{
				responseData.buildResult(ResponseEnum.DELETE_FAILUE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.failue();
		}
		return responseData;
	}
	
	/**
	 * 分页得到站内信列表
	 * @return PageView<SiteMsg>
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping(value="/getSiteMsgList.do",method=RequestMethod.POST)
	public Map<String,Object> getSiteMsgList(SiteMsgForm siteMsgForm){
		LogUtils.info("getSiteMsgList param:"+siteMsgForm.toString());
		Map<String,Object> gridMap = new HashMap<String,Object>();
		PageView<SiteMsg> pageView = null;
		try {
			String reciveAdminArr = "["+AdminCacheUtils.getLoginAdminUser().getId()+":"+AdminCacheUtils.getLoginAdminUser().getRealName()+"]";
			siteMsgForm.setReciveAdminArr(reciveAdminArr);
			pageView = this.siteMsgService.getSiteMsgList(siteMsgForm);
			gridMap.put("total", pageView.getTotalRecord());
			gridMap.put("rows", pageView.getRecords());
		} catch (Exception e) {
			e.printStackTrace();
			LogUtils.error("getSiteMsgList err:",e);
		}
		LogUtils.info("getSiteMsgList result:"+pageView.toString());
 		return gridMap;
	}
	
	/**
	 * 查询消息列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getSiteMsgListLimit15.do")
	public List<Map<String,Object>> getSiteMsgListLimit15(){
		LogUtils.info("getSiteMsgListLimit15 comming!");
		List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
		try {
			String reciveAdminArr = "["+AdminCacheUtils.getLoginAdminUser().getId()+":"+AdminCacheUtils.getLoginAdminUser().getRealName()+"]";
			String recivePositionArr = "["+AdminCacheUtils.getLoginAdminUser().getPositionUUID()+":"+AdminCacheUtils.getLoginAdminUser().getPositionName()+"]";
			mapList = this.siteMsgService.getSiteMsgListLimit15(reciveAdminArr,recivePositionArr);
		} catch (Exception e) {
			LogUtils.error("getSiteMsgListLimit15 err:",e);
		}
		LogUtils.info("getSiteMsgListLimit15 result:"+mapList);
 		return mapList;
	}
	
	/**
	 * 查询站内信详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/getSiteMsgDetail.do")
	public ModelAndView getSiteMsgDetail(int id,String actionType) {
		ModelAndView mv = new ModelAndView();
		SiteMsg bean = siteMsgService.queryForBeanById(SiteMsg.class, id);
		bean.setIsRead(1);
		bean.setReadCount(bean.getReadCount()+1);
		this.siteMsgService.merge(bean);
		try {
			mv.addObject("siteMsg",bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(StringUtils.equals(actionType,"update")){
			mv.setViewName("/adminMain/siteMsg/siteMsg-add.jsp");
		}else{
			mv.setViewName("/manage/siteMsg/siteMsgDetail.jsp");
		}
		return mv;
	}
}

